package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.TakeStockBean;
import com.sanyang.pda.bean.TakeStockListBean;
import com.sanyang.pda.bean.UploadBean;
import com.sanyang.pda.bean.request.TaskStockCompleteRequest;
import com.sanyang.pda.contract.StockContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class StockPresenter extends BasePresenter<StockContract.View> implements StockContract.Presenter {

    @Override
    public void getTaskStockComplete(TaskStockCompleteRequest request) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(request));
        NetUtils.geApi().inventoryComplete(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<TakeStockListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<TakeStockListBean> takeStockListBeanBaseAppEntity) throws Exception {
                        getView().showMessage(takeStockListBeanBaseAppEntity.getMsg());
                        getView().onStockSucces();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void stockList(String storagebinId,String taskNo) {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("storagebinId",storagebinId);
            jsonObject.put("taskNo",taskNo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), jsonObject.toString());
        NetUtils.geApi().commodityInventoryInformation(storagebinId,taskNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<TakeStockBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<TakeStockBean>> listBaseRowsAppEntity) throws Exception {
                        getView().setList(listBaseRowsAppEntity.getRows());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void uploadImage(String path) {
        File file=new File(path);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        NetUtils.geApi().uploadImage(part)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UploadBean>>() {
                    @Override
                    public void accept(BaseAppEntity<UploadBean> searchStockDetailBeanBaseAppEntity) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
